<?php
include("../includes/header-member.php");
?>
<title>LazySwim</title>
<link rel="stylesheet" href="../../includes/blueTable.css" type="text/css" media="screen" />
</head>

<body>
<div id="page">
    <div id="page-top">
        <div id="page-bottom">
          <div id="header">
                <div id="header-info">
                    <h1>LAZYSWIM</h1>
                    <div class="description">Simplistic Swim System</div>
                </div>
				<!-- Header Goes Here -->
                <?php printHeader(); ?>
				<!-- Header Ends Here -->
                <div id="header-date">Today is <?php echo date('l, F jS, Y'); ?></div>
                <div id="header-cats">
				<!-- Welcome Message Starts Here -->
                    <ul>
					<?php include("../../includes/welcomeBar.php"); ?>
					</ul>
				<!-- Welcome Message Ends Here -->	
                </div>
            </div>
            <div id="main">

                <div id="sidebar">
                    <?php
					// include the corresponding navigation page
                    include("../../includes/navigation.php");
					?>     
                </div>

<!-- main body -->
            
			<div class="featured-post"> 
			
				<div class="post-title"><h2> Pay For Meets </h2></div>
				<div class="post-entry">            
                <?php
                    $success = $_GET['auth'];
                    $userID;
                    if( isset($_GET['childID']) ) {
                        $parseGet = explode("?", $_GET['childID']);
                        $userID = $parseGet[0];
                        $success   = $parseGet[1];
                        $retURL = "http://www.hungmun.com/c410/site-gui/members/eventSignup/meetsBilling.php?childID=" . $userID;
                        $child = mysql_query("SELECT * FROM accounts WHERE userID = '$userID]'");
                        $child = mysql_fetch_assoc($child);
                        echo "$child[name]'s Meets:<br><br>";
                        echo "<a href=\"meetsBilling.php\">Return to Personal Meets Bills</a><br><br>";
                    }
                    else {
                        $userID = $_SESSION['ls_userid'];
                        $retURL = "http://www.hungmun.com/c410/site-gui/members/eventSignup/meetsBilling.php";
                    }
                    
                    // if we have come back from paybuddy update
                    if($success) {
                        mysql_query("UPDATE meetsBilling SET status = 'PAID' WHERE userID = '$userID'") or die( mysql_error());
                    }
                    
                    $child_sql = mysql_query("SELECT * FROM parentChild where parentID = '$userID'") or die(mysql_error());	
                    $children = mysql_num_rows($child_sql);
                    if( $children > 0 ) {
                        while( $relation = mysql_fetch_assoc($child_sql) ) {
                            $child = mysql_query("SELECT * FROM accounts WHERE userID = '$relation[childID]'");
                            $child = mysql_fetch_assoc($child);
                            echo '<br><a href=\'meetsBilling.php?childID='.$relation[childID].'\'>Pay for '
                                 . $child[name] . '\'s meets!</a>';
                        }
                    }	
                    
                    $sql = mysql_query("SELECT * FROM meetsBilling WHERE userID = '$userID' AND status = 'UNPAID'") or die(mysql_error());
                    if( mysql_num_rows($sql) == 0 ) {
                        echo "<br><br>No meets to pay for.<br>";
                    }
                    else {
                ?>
                        <table id='blueTable'><tr><th>Meet Name</th><th>Location</th><th>Date</th><th>Price</th></tr>
                        
                        <?php                    
                            $user = mysql_query("SELECT * FROM accounts WHERE userID = '$userID'") or die(mysql_error());
                            $user = mysql_fetch_assoc($user);
                            
                            $total = 0;
                            $transactionID = $userID . time();
                            
                            $i = 0;
                            while( $bill = mysql_fetch_assoc($sql) ) {            
                                $meet = mysql_query("SELECT * FROM meets WHERE meetID = '$bill[meetID]'") or die(mysql_error());
                                $meet = mysql_fetch_assoc($meet);
                                $style = (($i % 2) == 0)? 'even' : 'odd';
                                echo "<tr class='$style'><td>" . $meet[name] . "</td><td>" . $meet['location'] . "</td>";
                                echo "<td>" . $meet[date] . "</td><td>$" . sprintf("%01.2f", $meet[price]) . "</td></tr>";
                                $i++;
                                $total += $meet[price];
                            }        
                               
                        ?>
                        <tr><th>Total Cost:</th><td></td><td></td><td>$<?php echo sprintf("%01.2f", $total); ?></td></tr>
                        </table>
                        
                        <br><br>
                         <form name="payForm"
                         action="http://consort.cs.ualberta.ca/~c410/paybuddy/payment.cgi?grp=4&amt=<?php
                          echo sprintf("%01.2f", $total); ?>&tx=<?php echo $transactionID;?>&ret=<?php echo $retURL; ?>"
                          method="post"  target="_blank">
                            <input type="submit" name="submit" value="Pay for Meets">
                        </form>

                <?php 
                     }
                ?>
				<!-- end of main -->

				</div>
			</div>	
				

             
                <div class="clear"></div>

            </div>
			<!-- Footer Goes Here -->
            <?php include("../../includes/footer.php"); ?>
			<!-- Footer Ends Here -->
        </div>
    </div>
</div>
</body>
</html>